package com.weiku.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

public class PsqlConnectionTool {
	final static Logger log = LoggerFactory.getLogger(PsqlConnectionTool.class);
	@Value("${spring.datasource.url}")
	private String url="jdbc:postgresql://117.158.175.178:5432/weiku";
	@Value("${spring.datasource.username}")
	private String username="postgres";
	@Value("${spring.datasource.password}")
	private String password="W19k4u21798de";
    private Connection connection = null;
   
    public Connection getConn() {
        try {
            Class.forName("org.postgresql.Driver").newInstance();
            connection = DriverManager.getConnection(url, username, password);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public ResultSet query(Connection conn, String sql) {
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        try {
            pStatement = conn.prepareStatement(sql);
            rs = pStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        } 
        return rs;
    }

    public boolean queryUpdate(Connection conn, String sql) {
        PreparedStatement pStatement = null;
        int rs = 0;
        try {
            pStatement = conn.prepareStatement(sql);
            rs = pStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (rs > 0) {
            return true;
        }
        return false;
    }
    
    public static void insertRecepit(receipt receipt) {
		PsqlConnectionTool pgtool = new PsqlConnectionTool();
        Connection myconn = pgtool.getConn();
        String sql="insert into receipt ( "
        		+ " express_number,order_id, content,receipt_date,receipt_type"
        		+") values ('"+
        		receipt.getExpressNumber()
        	    +"','"+receipt.getOrderId()
        		+"','"+receipt.getContent()
        		+"','"+receipt.getReceiptDate()
        		+"','"+receipt.getReceiptType()
        			    +"')";
        System.out.println(sql);
        pgtool.queryUpdate(myconn, sql);
        log.info("插入数据库成功"+sql);
        System.out.println("插入数据库成功");
	}
    
    public static void insertSendSuccess(SendSuccess sendSuccess){
		PsqlConnectionTool pgtool = new PsqlConnectionTool();
        Connection myconn = pgtool.getConn();
        String sql="insert into send_success ("
                 +"order_id, xml, send_time, add_time"
        		+") values ('"
                 +sendSuccess.getOrderId()
        		+"','"+sendSuccess.getXml()
        		+"','"+sendSuccess.getSendTime()
        		+"','"+sendSuccess.getAddTime()
        			    +"')";
        System.out.println(sql);
        pgtool.queryUpdate(myconn, sql);
        log.info("插入数据库成功"+sql);
        System.out.println("插入数据库成功");
	}    
  
    public static double selectOrdersumByOrderId(String orderId) throws SQLException {
    	PsqlConnectionTool pgtool = new PsqlConnectionTool();
        Connection myconn = pgtool.getConn();
        ResultSet rs = pgtool.query(myconn, "select * from orderhead  where orderid= '"+orderId+"'");
        System.out.println(rs);
        double order_sum=0.0;
        while(rs.next()){            
        	String order_id = rs.getString("orderid");
            order_sum = rs.getDouble("ordersum");
            log.info("订单Id："+order_id+" 订单价格："+order_sum);
            System.out.println("订单Id："+order_id+" 订单价格："+order_sum);
            myconn.close();
        }
        System.out.println("查询数据库成功");
		return order_sum;
	}

	public static double selectSendSuccessByOrderId(String string) throws SQLException {
		PsqlConnectionTool pgtool = new PsqlConnectionTool();
        Connection myconn = pgtool.getConn();
        ResultSet rs = pgtool.query(myconn, "select * from send_success  where order_id= '"+string+"'");
        System.out.println(rs);
        double sum=0.0;
        while(rs.next()){            
        	String order_id = rs.getString("order_id");
            sum = rs.getDouble("sum");
            log.info("订单Id："+order_id+" 所有订单价格："+sum);
            System.out.println("订单Id："+order_id+" 所有订单价格："+sum);
            myconn.close();
        }
        System.out.println("查询数据库成功");
		return sum;
	}
}